const { localFileSystem } = require('uxp').storage ;
const photoshop = require('photoshop') ;
const { app, core } = photoshop ;

const saveAsJPEG = async (jpgQuality = 6) => {
  // Prompt the user to select a save location
  const entry = await localFileSystem.getFileForSaving('output.jpg') ;
  if(!entry) {
    console.error('No location selected for saving.') ;
    return ;
  }

  const doc = app.activeDocument ;
  if(!doc) {
    console.error('There is no target document.') ;
    return ;
  }

  await doc.saveAs.jpg(entry, {quality: jpgQuality}, true) ;
}

const test1 = async () => {
  try {
    await core.executeAsModal(
      async (control) => {
        await saveAsJPEG(7) ;
      }, 

      {
        'commandName': 'Save as JPEG',
        'interactive': true
      }
    ) ;
  } catch(e) {
    await app.showAlert(e) ;
  }
} ;
document.getElementById('testBtn1').addEventListener('click', () => { test1(); });